import pylab as plt
import os
import math
from params import *

hop_file = os.path.join(WORKDIR, 'gowalla', 'temporal', "results",'hop_probs.txt')
geohop_file = os.path.join(WORKDIR, 'gowalla', 'temporal', "results",'geohop_probs.txt')
x,y,y2 = [],[],[]
for line in open(hop_file):
    hop,num,den,prob = map(float,line.strip().split())
    hop = int(hop)
    if hop == -1:
        x0 = 0
        y0 = num
        continue
    x.append(hop)
    y.append(prob)
    y2.append(num)

plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
#plt.semilogy(x,y,'kx',mfc='None')
plt.semilogy(x,y,'ko', ms=6)
plt.xlabel(r"$h$")
plt.ylabel(r"$P_h$")
plt.grid(True)
plt.savefig('link_probhops.pdf')
plt.close()

plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
plt.semilogy(x,y2,'ko', ms=6)
plt.semilogy([x0],[y0], 'ko', ms=6)
xfit = x
yfit = map(math.log10,y2)
a,b = plt.polyfit(xfit,yfit,1)
fit = plt.polyval([a,b],xfit)
fit = map(lambda i:10**i,fit)
fit_leg = '$E_h \propto e^{%.2f h}$'%a
fx = plt.semilogy(xfit,fit,'k-',linewidth=1)
plt.legend( [fx[0]], [fit_leg])
plt.xlabel(r"$h$")
plt.ylabel(r"$E_h$")
plt.grid(True)
plt.savefig('link_hops.pdf')
plt.close()

#x,y,y2 = [],[],[]
#for line in open(geohop_file):
#    distance,num,den,prob = map(float,line.strip().split())
#    if distance >= 1e-2:
#        x.append(distance)
#        y.append(prob)
#        y2.append(num)
#
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,y,'ko',mfc='None')
#plt.xlabel(r"$d$")
#plt.ylabel(r"$P_d$")
#plt.grid(True)
#plt.xlim(xmin=1e-2)
#plt.savefig('geolink_probhops.pdf')
#plt.close()
#
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,y2,'ko',mfc='None')
##xfit = x
##yfit = map(math.log10,y2)
##a,b = plt.polyfit(xfit,yfit,1)
##fit = plt.polyval([a,b],xfit)
##fit = map(lambda i:10**i,fit)
##fit_leg = '$\propto e^{%.2f h}$'%a
##fx = plt.semilogy(xfit,fit,'k-',linewidth=2)
##plt.legend( [fx], [fit_leg])
#plt.xlabel(r"$d$")
#plt.ylabel(r"$E_d$")
#plt.grid(True)
#plt.xlim(xmin=1e-2)
#plt.savefig('geolink_hops.pdf')
#plt.close()
